package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.R.id;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.R.menu;
import com.example.mobifin_busticket.controller.databasemanager.DatabaseManager;
import com.example.mobifin_busticket.models.BusModel;
import com.example.mobifin_busticket.models.FilterBusModel;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class FilterBusActivity extends MobifinActivity implements
		android.view.View.OnClickListener {
	TextView tvroute, tvTravel, tvBoardingPoint, tvDropingPoint;
	Button btn_ac, btn_nonAc, btn_sleeper, btn_nonSleeper, btn_done,
			btn_clr_filter;
	ImageView ivExitFilter;
	ArrayList<BusModel> busList;
	String strAc = "", strSleeper = "", strNonAc = "", strNonSleeper = "";
	boolean isAC = false, isSleeper = false, isNonAc = false,
			isNonSleeper = false;
	ArrayList<String> Travelresult = null;
	ArrayList<String> Boardingresult = null;
	ArrayList<String> Dropingresult = null;
	ArrayList<String> selectTravelList = null, selectBoardingList = null,
			selectdropingList = null;
	Bundle mBundle;
	boolean isTravel = false, filterValue = true;
	FilterBusModel filterModel;
	int flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter_bus);
		filterModel = new FilterBusModel();
		mBundle = getIntent().getExtras();
		tvroute = (TextView) findViewById(R.id.txt_filter_rout);
		tvTravel = (TextView) findViewById(R.id.tvTravel);
		tvBoardingPoint = (TextView) findViewById(R.id.tvBoerdingPoint);
		tvDropingPoint = (TextView) findViewById(R.id.tvDropingPoint);
		btn_ac = (Button) findViewById(R.id.btn_ac);
		btn_nonAc = (Button) findViewById(R.id.btn_nonac);
		btn_sleeper = (Button) findViewById(R.id.btn_sleeper);
		btn_nonSleeper = (Button) findViewById(R.id.btn_nonsleeper);
		btn_done = (Button) findViewById(R.id.btn_done);
		btn_clr_filter = (Button) findViewById(R.id.btn_clr_all_filter);
		ivExitFilter = (ImageView) findViewById(R.id.img_cncl);

		tvTravel.setOnClickListener(this);
		tvBoardingPoint.setOnClickListener(this);
		tvDropingPoint.setOnClickListener(this);

		btn_ac.setOnClickListener(this);
		btn_nonAc.setOnClickListener(this);
		btn_sleeper.setOnClickListener(this);
		btn_nonSleeper.setOnClickListener(this);
		btn_done.setOnClickListener(this);
		btn_clr_filter.setOnClickListener(this);
		ivExitFilter.setOnClickListener(this);
		mBundle = getIntent().getExtras();
		String from = mBundle.getString("Source Name");
		String to = mBundle.getString("Destination Name");
		filterValue = mBundle.getBoolean("FilterValue");
		tvroute.setText(from + " " + " to" + " " + to);
		System.out
				.println("Filter value from FilterBUsActivity:" + filterValue);
		mInitializeFilter();
		mfilterStateMaintain();
	}

	@SuppressWarnings("deprecation")
	private void mfilterStateMaintain() {
		if (filterValue) {

			if (AppData.isAc) {
				System.out.println("ISAC value from AppData:" + AppData.isAc);
				strAc = "true";
				btn_ac.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
			}
			if (AppData.isNonAc) {
				strNonAc = "true";
				btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
			}
			if (AppData.isSleeper) {
				strSleeper = "true";
				btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
			}
			if (AppData.isNonSleeper) {
				strNonSleeper = "true";
				btn_nonSleeper.setBackgroundDrawable(getResources()
						.getDrawable(R.color.button_selected_filter_color));
			}
			if (AppData.flag == 1) {
				if (AppData.TravelResult != null) {
					Travelresult = AppData.TravelResult;
					tvTravel.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.filter_travels_applied, 0,
							R.drawable.filter_select_arrow, 0);
				}
			}
			if (AppData.flag == 2) {
				if (AppData.Boardingresult != null) {
					Boardingresult = AppData.Boardingresult;
					tvBoardingPoint.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.location_filter_selected, 0,
							R.drawable.filter_select_arrow, 0);
				}
			}
			if (AppData.flag == 3) {
				if (AppData.Dropingresult != null) {
					Dropingresult=AppData.Dropingresult;
					tvDropingPoint.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.location_filter_selected, 0,
							R.drawable.filter_select_arrow, 0);
				}
			}
		}
	}

	private void mInitializeFilter() {

		isAC = mBundle.getBoolean("Ac vale");
		isNonAc = mBundle.getBoolean("NonAc vale");
		isSleeper = mBundle.getBoolean("Sleeper");
		isNonSleeper = mBundle.getBoolean("NonSleepreValue");
		Travelresult = mBundle.getStringArrayList("TravelResult");
		Boardingresult = mBundle.getStringArrayList("BoardingResult");
		Dropingresult = mBundle.getStringArrayList("DropingResult");
		AppData.isAc = isAC;
		AppData.isNonAc = isNonAc;
		AppData.isSleeper = isSleeper;
		AppData.isNonSleeper = isNonSleeper;
		AppData.TravelResult = Travelresult;
		AppData.Boardingresult = Boardingresult;
		AppData.Dropingresult = Dropingresult;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ac:
			OnClickAc();
			break;
		case R.id.btn_nonac:
			OnClickNonAc();
			break;
		case R.id.btn_sleeper:
			OnClickSleeper();
			break;
		case R.id.btn_nonsleeper:
			OnClickNonSleeper();
			break;
		case R.id.tvTravel:
			OnClickTravel();
			break;
		case R.id.tvBoerdingPoint:
			OnClickBoardingPoint();
			break;
		case R.id.tvDropingPoint:
			OnClickDropingPoint();
			break;
		case R.id.btn_done:
			OnClickDone();
		case R.id.img_cncl:
			finish();
			overridePendingTransition(R.anim.hold, R.anim.push_out_to_bottom);
			break;
		case R.id.btn_clr_all_filter:
			onClearAllFilter();

			break;
		default:
			break;
		}
	}

	private void OnClickTravel() {
		// TODO Auto-generated method stub
		overridePendingTransition(R.anim.hold, R.anim.push_out_to_bottom);
		Intent TravelIntent = new Intent(FilterBusActivity.this,
				FilterBusByProperty.class);

		String travel = getResources().getString(R.string.filter_by_travels);
		TravelIntent.putExtra("dyanamicValue", travel);
		startActivityForResult(TravelIntent, 1);

	}

	private void OnClickBoardingPoint() {
		// TODO Auto-generated method stub
		overridePendingTransition(R.anim.hold, R.anim.push_out_to_bottom);
		Intent BoardingPointIntent = new Intent(FilterBusActivity.this,
				FilterBusByProperty.class);

		String Boarding = getResources().getString(R.string.filter_by_boarding);
		BoardingPointIntent.putExtra("dyanamicValue", Boarding);
		startActivityForResult(BoardingPointIntent, 2);
	}

	private void OnClickDropingPoint() {
		// TODO Auto-generated method stub
		overridePendingTransition(R.anim.hold, R.anim.push_out_to_bottom);
		Intent dropingPointIntent = new Intent(FilterBusActivity.this,
				FilterBusByProperty.class);

		String droping = getResources().getString(R.string.filter_by_droping);
		dropingPointIntent.putExtra("dyanamicValue", droping);
		startActivityForResult(dropingPointIntent, 3);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == 1) {
			Travelresult = new ArrayList<String>();

			Travelresult = data.getStringArrayListExtra("FilterByProperty");

			if (!Travelresult.isEmpty()) {
				flag = 1;
				AppData.flag = flag;
				AppData.TravelResult = Travelresult;
				 tvTravel.setCompoundDrawablesWithIntrinsicBounds(
                         R.drawable.filter_travels_applied, 0,
                         R.drawable.filter_select_arrow, 0);
			} else {
				tvTravel.setCompoundDrawablesWithIntrinsicBounds(
                         R.drawable.travels, 0,
                         R.drawable.filter_select_arrow, 0);
				Travelresult=null;
			}
			System.out.println("Travels in filter:" + Travelresult);
		}
		if (resultCode == RESULT_OK && requestCode == 2) {
			Boardingresult = new ArrayList<String>();
			selectBoardingList = data
					.getStringArrayListExtra("FilterByProperty");
			Boardingresult.addAll(selectBoardingList);
			flag = 2;
			AppData.flag = flag;
			AppData.Boardingresult = Boardingresult;
			if (!Boardingresult.isEmpty()) {
				tvBoardingPoint.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.location_filter_selected, 0,
						R.drawable.filter_select_arrow, 0);
			} else {
				tvBoardingPoint.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.location_filter_active, 0,
						R.drawable.filter_select_arrow, 0);
				Boardingresult=null;

			}
			System.out.println("Boarding in filter:" + Boardingresult);
		}
		if (resultCode == RESULT_OK && requestCode == 3) {
			Dropingresult = new ArrayList<String>();
			selectdropingList = data
					.getStringArrayListExtra("FilterByProperty");

			Dropingresult.addAll(selectdropingList);
			flag = 3;
			AppData.flag = flag;
			AppData.Dropingresult = Dropingresult;
			if (!Dropingresult.isEmpty()) {
				tvDropingPoint.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.location_filter_selected, 0,
						R.drawable.filter_select_arrow, 0);
			} else {
				tvDropingPoint.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.location_filter_active, 0,
						R.drawable.filter_select_arrow, 0);
				Dropingresult=null;
			}
			System.out.println("Droping in filter:" + Dropingresult);
		}

	}

	@SuppressWarnings("deprecation")
	private void OnClickAc() {
		// TODO Auto-generated method stub
		if (btn_ac.isClickable()) {
			if (isAC == false) {
				isAC = true;
				AppData.isAc = isAC;
				btn_ac.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
				btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));

				strAc = "true";
				strNonAc = "false";
				AppData.isNonAc=false;

			} else {
				isAC = false;
				AppData.isAc = isAC;
				btn_ac.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));

				strAc = "false";
				strNonAc = "false";
				AppData.isNonAc=false;
			}

		}

	}

	@SuppressWarnings("deprecation")
	private void OnClickNonAc() {
		// TODO Auto-generated method stub
		if (btn_nonAc.isClickable()) {
			if (isNonAc == false) {
				isNonAc = true;
				AppData.isNonAc = isNonAc;
				btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
				btn_ac.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				isAC = false;
				strAc = "false";
				strNonAc = "true";
				AppData.isAc=false;
			} else {
				isNonAc = false;
				AppData.isNonAc = isNonAc;
				btn_ac.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				strAc = "false";
				AppData.isAc=false;
				strNonAc = "false";
			}

		}

	}

	@SuppressWarnings("deprecation")
	private void OnClickNonSleeper() {
		// TODO Auto-generated method stub

		if (btn_nonSleeper.isClickable()) {
			if (isNonSleeper == false) {
				isNonSleeper = true;
				AppData.isNonSleeper = isNonSleeper;
				btn_nonSleeper.setBackgroundDrawable(getResources()
						.getDrawable(R.color.button_selected_filter_color));
				btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				isSleeper = false;
				strNonSleeper = "true";
				strSleeper = "false";
				AppData.isSleeper=false;
			} else {
				isNonSleeper = false;
				AppData.isNonSleeper = isNonSleeper;
				btn_nonSleeper.setBackgroundDrawable(getResources()
						.getDrawable(R.color.button_color_filter));
				btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));

				strNonSleeper = "false";
				strSleeper = "false";
				AppData.isSleeper=false;
			}

		}

	}

	@SuppressWarnings("deprecation")
	private void OnClickSleeper() {
		// TODO Auto-generated method stub
		if (btn_sleeper.isClickable()) {
			if (isSleeper == false) {
				isSleeper = true;
				AppData.isSleeper = isSleeper;
				btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_selected_filter_color));
				btn_nonSleeper.setBackgroundDrawable(getResources()
						.getDrawable(R.color.button_color_filter));

				strSleeper = "true";
				strNonSleeper = "false";
				AppData.isNonSleeper=false;
			} else {
				isSleeper = false;
				AppData.isSleeper = isSleeper;
				btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
						R.color.button_color_filter));
				btn_nonSleeper.setBackgroundDrawable(getResources()
						.getDrawable(R.color.button_color_filter));

				strSleeper = "false";
				strNonSleeper = "false";
				AppData.isNonSleeper=false;
			}

		}

	}

	private void OnClickDone() {
		// TODO Auto-generated method stub
		if (btn_ac.isClickable() || btn_nonAc.isClickable()
				|| btn_sleeper.isClickable() || btn_nonSleeper.isClickable()) {
			isAC = true;
			isSleeper = true;
			Intent returnIntent = new Intent();
			returnIntent.putExtra("AC", strAc);
			returnIntent.putExtra("NonAC", strNonAc);
			returnIntent.putExtra("Sleeper", strSleeper);
			returnIntent.putExtra("NonSleeper", strNonSleeper);
			returnIntent.putStringArrayListExtra("retuentravelList",
					Travelresult);
			returnIntent.putStringArrayListExtra("retuenBoardingList",
					Boardingresult);
			returnIntent.putStringArrayListExtra("retuenDropingList",
					Dropingresult);
			setResult(RESULT_OK, returnIntent);
			finish();
		}

	}

	@SuppressWarnings("deprecation")
	private void onClearAllFilter() {
		// TODO Auto-generated method stub

		if (btn_ac.isClickable()) {
			strAc="false";
			btn_ac.setBackgroundDrawable(getResources().getDrawable(
					R.color.button_color_filter));
		}
		if (btn_nonAc.isClickable()) {
			strNonAc="false";
			btn_nonAc.setBackgroundDrawable(getResources().getDrawable(
					R.color.button_color_filter));
		}
		if (btn_sleeper.isClickable()) {
			strSleeper="false";
			btn_sleeper.setBackgroundDrawable(getResources().getDrawable(
					R.color.button_color_filter));
		}
		if (btn_nonSleeper.isClickable()) {
			strNonAc="false";
			btn_nonSleeper.setBackgroundDrawable(getResources().getDrawable(
					R.color.button_color_filter));
		}

		if (Travelresult != null) {
			tvTravel.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.travels, 0, R.drawable.filter_select_arrow, 0);
			Travelresult=null;

		}
		if (Boardingresult != null) {
			tvBoardingPoint.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.location_filter_active, 0,
					R.drawable.filter_select_arrow, 0);
			Boardingresult=null;

		}
		if (Dropingresult != null) {
			tvDropingPoint.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.location_filter_active, 0,
					R.drawable.filter_select_arrow, 0);
			Dropingresult=null;

		}

	}
}
